package com.chengzhi.service;

import com.chengzhi.dto.StorageMapper;
import com.chengzhi.entity.Storage;
import io.seata.core.context.RootContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @description:
 * @author: ruanchengzhi
 * @create: 2020-06-22 19:28
 **/
@Service
public class StorageService {
    private static final Logger logger = LoggerFactory.getLogger(StorageService.class);
    @Resource
    private StorageMapper storageMapper;

    @Transactional
    public void deduct(String commodityCode, int count) {
        logger.info("开始全局事务，XID = " + RootContext.getXID());
        Storage storage1 = new Storage();
        storage1.setCommodityCode(commodityCode);
        Storage storage = storageMapper.selectOne(storage1);
        storage.setCount(storage.getCount() - count);
        storageMapper.updateByPrimaryKey(storage);
    }
}
